package com.stu.day13.listsort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author zy
 * @date 2025/2/11 14:55
 */
public class ListSortDemo1 {
    public static void main(String[] args) {
        Order o1 = new Order(1, "1001", 100.0d);
        Order o2 = new Order(2, "1002", 200.0d);
        Order o3 = new Order(3, "1003", 100.0d);
        Order o4 = new Order(4, "1004", 300.0d);
        Order o5 = new Order(5, "1005", 400.0d);

        ArrayList<Order> list = new ArrayList<>();
        list.add(o1);
        list.add(o2);
        list.add(o3);
        list.add(o4);
        list.add(o5);

        //在JDK8.0之前,只能使用java.util.Collections集合工具类
        //TODO 集合中的对象必须要实现Comparable接口,并且重写compareTo方法
        Collections.sort(list);

        //遍历集合
        for (Order order : list) {
            System.out.println(order);
        }
    }
}

class Order implements Comparable<Order> {
    private Integer id;
    private String ordNO;
    private Double total;

    public Order() {
    }

    public Order(Integer id, String ordNO, Double total) {
        this.id = id;
        this.ordNO = ordNO;
        this.total = total;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOrdNO() {
        return ordNO;
    }

    public void setOrdNO(String ordNO) {
        this.ordNO = ordNO;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Order order = (Order) o;
        return Objects.equals(id, order.id) && Objects.equals(ordNO, order.ordNO);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, ordNO);
    }

    @Override
    public String toString() {
        String sb = "Order{" + "id=" + id +
                ", ordNO='" + ordNO + '\'' +
                ", total=" + total +
                '}';
        return sb;
    }

    @Override
    public int compareTo(Order o) {
        if (this.total > o.total)
            return -1;
        else if (this.total < o.total)
            return 1;
        return 0;
    }
}
